package com.cpumonitor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Utils
 * Description: TODO
 * User: kevin
 * Date: 2018-11-21
 * Time: 下午5:55
 */
public class Utils {

    /**
     * @Descriptions:获取自字符串，避免中文的双字节问题
     * @param src
    * @param start_index
    * @param end_index
     * @return: java.lang.String
     * @Author: kevin
     * @date: 18-11-21 下午11:46
     */
    public static String substring(String src, int start_index, int end_index) {

        byte[] b = src.getBytes();
        String sstr = "";
        for (int i = start_index; i <= end_index; i++) {
            sstr += (char) b[i];
        }
        return sstr;
    }

    /**
     * @Descriptions:ｆｏｒ　ｗｉｎｄｏｗｓ
     * 通过系统命令接口获得数据
     * @param system_command
     * @return: java.io.BufferedReader
     * @Author: kevin
     * @date: 18-11-23 下午9:39
     */
    public static BufferedReader getWinCommandResult(String system_command){
        //
        Process process = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader= null;
        if(!isLinux()){
            try {
                process = Runtime.getRuntime().exec(system_command);
                inputStreamReader = new InputStreamReader(process.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return bufferedReader;
    }

    /**
     * @Descriptions:ｆｏｒ　ｌｉｎｕｘ
     * 通过系统命令接口获得数据
     * @param system_commands
     * @return: java.io.BufferedReader
     * @Author: kevin
     * @date: 18-11-23 下午10:26
     */
    public static BufferedReader getLinuxCommandResult(String[] system_commands){

        Process process = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader= null;
        if(isLinux()){
            try {
                process = Runtime.getRuntime().exec(system_commands);
                inputStreamReader = new InputStreamReader(process.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        return bufferedReader;
    }

    /**
     * @Descriptions:获取ｌｉｎｕｘ　逻辑ｃｐｕ数(逻辑ｃｐｕ＝物理ｃｐｕ数　×　核数)
     * @param
     * @return: int
     * @Author: kevin
     * @date: 18-11-23 下午10:27
     */
    public static int getLinuxProcessorCount(){
        int processor_count = 0;
        String[] procCmd = null;
        BufferedReader br = null;
        String line = null;
        if(isLinux()){
            //获取ｌｉｎｕｘ逻辑ｃｐｕ数命令
            procCmd = getLinuxCommands("cat /proc/stat | grep -E \"cpu[0-9]\"|wc -l");
            br = getLinuxCommandResult(procCmd);

            try {
                //读出数据
                line = br.readLine();
                if(null != line) processor_count = Integer.valueOf(line).intValue();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return processor_count;
    }

    /**
     * @Descriptions:判断是否为ｌｉｎｕｘ
     * @param
     * @return: boolean
     * @Author: kevin
     * @date: 18-11-23 下午10:37
     */
    public static boolean isLinux(){

        Boolean isLinux = true;
        String osName = System.getProperty("os.name").toLowerCase();
        if(!osName.startsWith("linux")) isLinux = false;

        return isLinux;
    }

    /**
     * @Descriptions:预处理ｌｉｎｕｘ下的运行参数
     * @param cmd
     * @return: java.lang.String[]
     * @Author: kevin
     * @date: 18-11-23 下午10:29
     */

    public static String[] getLinuxCommands(String cmd){

        String[] cmds = new String[3];
        cmds[0] = "/bin/sh";
        cmds[1] = "-c";
        cmds[2] = cmd;
        return cmds;
    }
}
